package web.ssm.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import web.ssm.entity.Users;
import web.ssm.service.UsersService;

@Controller
public class UsersController {
	@Autowired
	private UsersService usersService;
	@RequestMapping("add")
	public String add(Users users,HttpServletRequest request){
		try {
			usersService.add(users);
			return "redirect:/getAll.do";
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "添加信息失败！具体异常信息：" + e.getMessage());
			return "userJsp/result";
		}
	}
	@RequestMapping("getAll")
	public String all(HttpServletRequest request){
		try {
			List<Users> users=usersService.all();
			request.setAttribute("addLists", users);
			return "userJsp/listAll";
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "信息载入失败！具体异常信息：" + e.getMessage());
			return "userJsp/result";
		}
	}
	@RequestMapping("addTo")
	public String addTo(){
		
		return "userJsp/addTo";
	}
	@RequestMapping("inquiry")
	public String inquiry(int id,HttpServletRequest request){
		Users users=usersService.inquiry(id);
		request.setAttribute("user",users);
		return "userJsp/inquiry";
	}
	@RequestMapping("update")
	public String update(Users users,HttpServletRequest request){
		try {
			usersService.update(users);
			return "redirect:/getAll.do";
		} catch (Exception e) {
			e.printStackTrace();
			request.setAttribute("InfoMessage", "信息载入失败！具体异常信息：" + e.getMessage());
			return "userJsp/result";
		}
		
	}
	@RequestMapping("delete")
	public String delete(int id){
		usersService.delete(id);
		return "redirect:/getAll.do";
	}
}
